package com.charonmc.listeners;

import com.charonmc.events.PlayerAfterLoginListener;
import com.charonmc.CharonLib;
import com.charonmc.utils.PrefixUtils;
import com.charonmc.utils.mysql.SqlManager;
import lombok.SneakyThrows;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerQuitEvent;
import org.bukkit.scheduler.BukkitRunnable;

import java.sql.ResultSet;

public class PlayerListenerLib implements Listener {
    @EventHandler
    public void onPlayerJoinLib(PlayerJoinEvent event){
        event.setJoinMessage("");
        BukkitRunnable queryPlayerNick = new BukkitRunnable() {
            @SneakyThrows
            @Override
            public void run() {
                SqlManager sqlManager = new SqlManager("charonmc");
                ResultSet set = sqlManager.createQuery()
                        .inTable("players")
                        .selectColumns("player_id")
                        .addCondition("player_name", event.getPlayer().getName())
                        .build()
                        .execute()
                        .getResult();
                if (set.next()){
                    Object playerId = set.getInt(1);
                    ResultSet result = sqlManager.createQuery()
                            .inTable("nick")
                            .selectColumns("nickname")
                            .addCondition("player_id", playerId)
                            .build()
                            .execute()
                            .getResult();
                    if (result.next()){
                        if (event.getPlayer().hasPermission("Core.Nick")){
                            Object nickname = sqlManager.createQuery()
                                    .inTable("nick")
                                    .selectColumns("nickname")
                                    .build()
                                    .execute()
                                    .first();
                            event.getPlayer().setDisplayName(result.getString(1));
                            event.getPlayer().setPlayerListName(result.getString(1));
                            //筛查nick
                        } else {
                            Player player = event.getPlayer();
                            sqlManager.createDelete()
                                    .inTable("nick")
                                    .addCondition("nickname",player.getDisplayName())
                                    .build()
                                    .execute();
                            player.setDisplayName(player.getName());
                            player.setPlayerListName(player.getName());
                        }
                    }
                    //称号
                    Object prefix = sqlManager.createQuery()
                            .inTable("player_set")
                            .selectColumns("prefix")
                            .addCondition("player_id", playerId)
                            .build()
                            .execute()
                            .first();
                    PrefixUtils.playerPrefix.put(event.getPlayer(), (Integer) prefix);
                } else {
                    PrefixUtils.playerPrefix.put(event.getPlayer(),  -1);
                }
                sqlManager.close();
                PlayerAfterLoginListener e = new PlayerAfterLoginListener(event.getPlayer());
                Bukkit.getServer().getPluginManager().callEvent(e);
            }
        };
        queryPlayerNick.runTaskAsynchronously(CharonLib.instance);
    }

}
